package task3.source;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyEditor;
import java.io.File;
import javax.swing.JFileChooser;

public class FileEditor
  extends Panel
  implements PropertyEditor
{
  private static final long serialVersionUID = 7305209224058792129L;
  private PropertyChangeSupport support;
  private JFileChooser _fileChooser;
  
  public FileEditor()
  {
    setLayout(new GridLayout(1, 1));
    
    this._fileChooser = new JFileChooser();
    
    this._fileChooser.setFileSelectionMode(2);
    this._fileChooser.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        FileEditor.this.support.firePropertyChange("", null, null);
      }
    });
    add(this._fileChooser);
    

    this.support = new PropertyChangeSupport(this);
  }
  
  public String getAsText()
  {
    if (this._fileChooser.getSelectedFile() != null) {
      return this._fileChooser.getSelectedFile().getAbsolutePath();
    }
    return " ";
  }
  
  public Component getCustomEditor()
  {
    return this;
  }
  
  public String getJavaInitializationString()
  {
    return null;
  }
  
  public String[] getTags()
  {
    return null;
  }
  
  public Object getValue()
  {
    return this._fileChooser.getSelectedFile().getAbsolutePath();
  }
  
  public boolean isPaintable()
  {
    return true;
  }
  
  public void paintValue(Graphics gfx, Rectangle arg1)
  {
    gfx.drawString("Click to choose File", 0, 22);
  }
  
  public void setAsText(String arg0)
    throws IllegalArgumentException
  {}
  
  public void setValue(Object arg0) {}
  
  public boolean supportsCustomEditor()
  {
    return true;
  }
  
  public void addPropertyChangeListener(PropertyChangeListener pcl)
  {
    this.support.addPropertyChangeListener(pcl);
  }
  
  public void removePropertyChangeListener(PropertyChangeListener pcl)
  {
    this.support.removePropertyChangeListener(pcl);
  }
}
